﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Text.RegularExpressions;

namespace Web.Page
{
    public partial class XSHTGLMIX_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改销售合同明细单";
                initValue();
            }
        }



        public static string xshtid { get; set; }
        public static string cpid { get; set; }
        public static string sl { get; set; }
        public static string dj { get; set; }
     

        private void initValue()
        {
            string id = Request.QueryString["id"];
            this.hdId.Value = id;
            XSHTMXB_Dao daoMx = new XSHTMXB_Dao();
            XSHTMXB mx = daoMx.Query(id);

           
            xshtid = mx.XSHTID.ToString();
            cpid = mx.CPID.ToString();
            sl = mx.SL.ToString();
            dj = mx.DJ.ToString();

        
            List<XSHTMXB> xsbmi = daoMx.QueryAll();
            
            //产品id
            List < object > cp = daoMx.QueryCPAll();
            this.ddlkpid.DataSource = cp;
            this.ddlkpid.DataTextField = "MC";
            this.ddlkpid.DataValueField = "ID";
            this.ddlkpid.DataBind();
            this.ddlkpid.Items.Insert(0, new ListItem("--------请选择--------", "0"));
            //数量
            this.tbSL.Text = sl;
            //this.ddlsl.DataSource = xsbmi;
            //this.ddlsl.DataTextField = "SL";
            //this.ddlsl.DataValueField = "ID";
            //this.ddlsl.DataBind();
            //this.ddlsl.Items.Insert(0, new ListItem("--------请选择--------", "0"));
            //单价
            this.tbDJ.Text = dj;
            //this.ddldj.DataSource = xsbmi;
            //this.ddldj.DataTextField = "DJ";
            //this.ddldj.DataValueField = "ID";
            //this.ddldj.DataBind();
            //this.ddldj.Items.Insert(0, new ListItem("--------请选择--------", "0"));
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {            

            this.ddlkpid.Text = cpid;
            this.tbSL.Text = sl ;
            this.tbDJ.Text = dj;
            this.lbResult.Text = "";
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {

            this.lbResult.Text = "";

            string str2 = @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
            Regex myRegex2 = new Regex(str2);

            string str3 = "^([0-9]*)$";
            Regex myRegex3 = new Regex(str3);
            if (ddlkpid.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入或选择产品";
                return;
            }
            if (!myRegex3.IsMatch(this.tbSL.Text.Trim()))
            {
                this.lbResult.Text = "请输入或选择数量";
                return;
            }
            if (!myRegex2.IsMatch(this.tbDJ.Text.Trim()))
            {
                this.lbResult.Text = "请输入或选择单价";
                return;
            }
            string b = Request.QueryString["id"];
            string a = Request.QueryString["xshtid"];
            XSHTMXB entity = new XSHTMXB()
            {
                ID = Guid.Parse(Request.QueryString["id"]),
                XSHTID = Guid.Parse(Request.QueryString["xshtid"]),
                CPID = Guid.Parse(ddlkpid.SelectedValue),
                SL = Convert.ToDouble(tbSL.Text),
                DJ = Convert.ToDouble(tbDJ.Text)                
            };

            string resultMessage = "";
            try
            {
                XSHTMXB_Dao daomxb = new XSHTMXB_Dao();
                int result = daomxb.Update(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";                    
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;               
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            string op = (string)Cache.Get(JCCD_Query.OP_KEY);
            Response.Redirect("~/Page/XSHTGL_Query.aspx?id=" + this.hdJCCDID.Value + "&op=" + op);
        }
    }
}